﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class ProductCateogryList : AdminBaseUsercontrol
	{
		private Service.ICategoryService _CategoryService;

		public int ProductID
		{
			get
			{
				return CommonHelper.QueryStringInt("productid");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindData();
		}

		protected void GridCategories_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.CateogryList.CategoryName");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.CateogryList.Description");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.CateogryList.CreatedOn");
				e.Row.Cells[4].Text = GetLocalResourceString("Admin.CateogryList.UpdatedOn");
				e.Row.Cells[5].Text = GetLocalResourceString("Admin.CateogryList.Published");
				e.Row.Cells[6].Text = GetLocalResourceString("Admin.CateogryList.DisplayOrder");
			}

			if (e.Row.DataItem != null)
			{
				var item = e.Row.DataItem as Data.Entity.Category;

				HyperLink HyperLinkCategory = e.Row.FindControl("HyperLinkCategory") as HyperLink;

				HyperLinkCategory.NavigateUrl = "~/Administration/EditProdcutCategory.aspx?categoryid=" + item.CategoryID.ToString();
				HyperLinkCategory.Text = item.CategoryName;

				if (item.ParentCategoryID > 0)
				{
					if (_CategoryService == null)
					{
						_CategoryService = new Service.CategoryService();
					}
					var parentCategory = _CategoryService.GetCategoryByID(item.ParentCategoryID);

					HyperLinkCategory.Text = parentCategory.CategoryName + "&nbsp;&nbsp;>>&nbsp;&nbsp;" + item.CategoryName;
				}
			}
		}

		private void BindData()
		{
			if (_CategoryService == null)
			{
				_CategoryService = new Service.CategoryService();
			}

			_CategoryService = new Service.CategoryService();
			var languageID = 0;

			if (SiteContext.Current.WorkingLanguage != null)
			{
				languageID = SiteContext.Current.WorkingLanguage.LanguageId;

				var categories = _CategoryService.GetCategoryByLanguage(languageID);

				GridCategories.DataSource = categories;
				GridCategories.DataBind();
			}
		}

		public void DeleteSelectedCategory()
		{
			if (_CategoryService == null)
			{
				_CategoryService = new Service.CategoryService();
			}

			_CategoryService = new Service.CategoryService();

			foreach (System.Web.UI.WebControls.GridViewRow rowItem in GridCategories.Rows)
			{
				var chBox = rowItem.FindControl("cbProductCategory") as CheckBox;
				var hfProductCategoryId = rowItem.FindControl("hfProductCategoryId") as HiddenField;

				bool isChecked = chBox.Checked;
				int categoryID = int.Parse(hfProductCategoryId.Value);

				if (isChecked)
				{
					_CategoryService.DeleteCategory(categoryID);
				}
			}

			//BindData();
		}
	}
}